package com.hxjr.dialog;

import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.util.DisplayMetrics;
import android.view.Gravity;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;
import android.widget.TextView;

import com.hxjr.dialog.adapter.SelectItemsAdapter;

import java.util.List;

public class SelectDialog extends Dialog
{
	private Context mContext = null;

	private ListView lv = null;

    private TextView mTvTitle = null;
    private TextView mTvCancel = null;
    private TextView mTvConfirm = null;
    private View mHeadLine = null;
    private View mFootLine = null;
    private View mFootSepLine = null;

	private MyListViewItemClickListener myListViewItemClickListener = null;

	private int type;

    private int height;

	public int getType()
	{
		return type;
	}

	public void setType(int type)
	{
		this.type = type;
	}

	public interface MyListViewItemClickListener
	{
		void onDialogItemSelected(int position, String str);
	}

	public SelectDialog(Context context, int theme)
	{
		super(context, theme);
		this.mContext = context;
        this.height = getDisplayMetrics().heightPixels / 2;
		initView();
	}

	public SelectDialog(int height, Context context)
	{
		super(context);
		this.mContext = context;
        this.height = height;
		initView();
	}

    public SelectDialog(Context context)
    {
        super(context);
        this.mContext = context;
        this.height = getDisplayMetrics().heightPixels / 2;
        initView();
    }

	private void initView()
	{

		getWindow().setWindowAnimations(R.style.AnimBottom);
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		getWindow().setBackgroundDrawableResource(android.R.color.transparent);
		setContentView(R.layout.select_item_dialog_layout);
		// setTitle(R.string.please_choose_car);
		WindowManager.LayoutParams lp = getWindow().getAttributes();
		lp.width = WindowManager.LayoutParams.WRAP_CONTENT;
//		lp.height = WindowManager.LayoutParams.WRAP_CONTENT;
		lp.height = this.height;
		getWindow().setAttributes(lp);
		getWindow().setGravity(Gravity.CENTER);
		setCanceledOnTouchOutside(true);
        mTvTitle = (TextView) findViewById(R.id.tv_dialog_title);
        mTvCancel = (TextView) findViewById(R.id.tv_dialog_cancel);
        mTvConfirm = (TextView) findViewById(R.id.tv_dialog_confirm);
        mFootLine = findViewById(R.id.foot_line);
        mFootSepLine = findViewById(R.id.foot_sep_line);
        mHeadLine = findViewById(R.id.head_line);

		lv = (ListView) findViewById(R.id.select_item_content);
		lv.setOnItemClickListener(new OnItemClickListener()
		{
			@Override
			public void onItemClick(AdapterView<?> arg0, View arg1,
					int position, long arg3)
			{
                if(myListViewItemClickListener != null){
                    myListViewItemClickListener.onDialogItemSelected(position, lv.getAdapter() != null ? (String) lv.getAdapter().getItem(position) : null);
                }
			}
		});
	}

    @Override
    public void setTitle(CharSequence title) {
        mTvTitle.setVisibility(View.VISIBLE);
        mHeadLine.setVisibility(View.VISIBLE);
        mTvTitle.setText(title);
    }

    @Override
    public void setTitle(int titleId) {
        mTvTitle.setVisibility(View.VISIBLE);
        mHeadLine.setVisibility(View.VISIBLE);
        mTvTitle.setText(titleId);
    }

    /**
     * 显示确认按钮
     * @param listener
     * @param confirmText
     */
    public void showConfirmButton(View.OnClickListener listener,String confirmText){
        mTvConfirm.setVisibility(View.VISIBLE);
        mFootSepLine.setVisibility(View.VISIBLE);
        mTvConfirm.setOnClickListener(listener);
        mTvConfirm.setText(confirmText);
    }

    /**
     * 显示取消按钮
     * @param listener
     * @param confirmText
     */
    public void showCancelButton(View.OnClickListener listener,String confirmText){
        mTvCancel.setVisibility(View.VISIBLE);
        mTvCancel.setOnClickListener(listener);
        mTvCancel.setText(confirmText);
    }

    public void setHeight(int height) {
        this.height = height;
    }

	public SelectDialog(Context context, boolean cancelable,
                        OnCancelListener cancelListener)
	{
		super(context, cancelable, cancelListener);
		this.mContext = context;
	}

	@Override
	protected void onCreate(Bundle savedInstanceState)
	{
		super.onCreate(savedInstanceState);

	}

	public void setData(List<String> datas)
	{
		lv.setAdapter(new SelectItemsAdapter(mContext, datas));
	}

	public void setListViewClickListener(MyListViewItemClickListener myListViewItemClickListener)
	{
		this.myListViewItemClickListener = myListViewItemClickListener;
	}


    /**
     * 获取屏幕的大小
     *
     * @return
     */
    public DisplayMetrics getDisplayMetrics() {
        DisplayMetrics metrics = new DisplayMetrics();
        ((WindowManager) mContext.getSystemService(Context.WINDOW_SERVICE))
                .getDefaultDisplay().getMetrics(metrics);
        return metrics;
    }
}
